package com.backend.system.beans.permission;

import cn.hutool.core.bean.BeanUtil;
import com.backend.system.dao.entity.Permission;
import com.backend.system.util.TimeUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.Set;

/**
 * 按角色查询全部权限（标识已拥有、未拥有）vo
 *
 * @author backend-manage
 */
@ApiModel
@Data
public class ListPermissionByRoleVO implements Serializable {
    @Serial
    private static final long serialVersionUID = 3819307789256495169L;

    /**
     * id
     */
    @ApiModelProperty("id")
    private Long id;

    /**
     * 权限编码
     */
    @ApiModelProperty("权限编码")
    private String code;

    /**
     * 权限类型
     */
    @ApiModelProperty("权限类型")
    private Integer type;

    /**
     * 权限类型(中文描述)
     */
    @ApiModelProperty("权限类型(中文描述)")
    private String typeStr;

    /**
     * 权限名
     */
    @ApiModelProperty("权限名")
    private String name;

    /**
     * 角色是否拥有此权限：true标识拥有
     */
    @ApiModelProperty("角色是否拥有此权限：true标识拥有")
    private Boolean selected;

    /**
     * 权限配置内容
     */
    @ApiModelProperty("权限配置内容")
    private String value;

    /**
     * 备注
     */
    @ApiModelProperty("备注")
    private String remark;

    /**
     * 创建时间
     */
    @ApiModelProperty("创建时间")
    @JsonFormat(pattern = TimeUtil.DEFAULT_DATE_TIME_PATTERN, timezone = TimeUtil.GMT_8)
    private LocalDateTime createTime;

    public static ListPermissionByRoleVO convert(Permission permission, Set<Long> permissionIds, Map<Integer, String> permissionTypeMap) {
        ListPermissionByRoleVO vo = new ListPermissionByRoleVO();
        BeanUtil.copyProperties(permission, vo);
        vo.setSelected(permissionIds.contains(permission.getId()));
        vo.setTypeStr(permissionTypeMap.get(vo.getType()));
        return vo;
    }
}
